package itsm.isperp.framework.utils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ibm.icu.text.SimpleDateFormat;

/**
 * 
 * 工作日历工具
 * 
 * @author lizx
 * @date 2013-5-8
 */
public abstract class WorkCalendarUtils {

	public static SimpleDateFormat YYYYMMDDFORMATE = new SimpleDateFormat(
			"yyyyMMdd");

	/**
	 * 根据工作日历接口 计算指定开始时间和结束时间的工作时间
	 * 
	 * @param wordCalendars
	 *            一个以“yyyy-MM-dd”格式的日期map
	 * @param date
	 *            // * @return
	 */
	public static void calSub(String from, String to) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date to1 = df.parse(to);
		Date from1 = df.parse(from);
		long l = to1.getTime() - from1.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		System.out.println("" + day + "天" + hour + "小时" + min + "分");
		double sum = day * 7.5;
		System.out.println(sum);
		Date t1 = new Date(from1.getYear(), from1.getMonth(), from1.getDate(),
				18, 00);
		Date t2 = new Date(to1.getYear(), to1.getMonth(), to1.getDate(), 8, 30);

		System.out.println(t1);
		System.out.println(t2);
		double temphour = 0;
		System.out.println(from1.getHours());
		if (from1.getHours() >= 14) {
			temphour = (t1.getTime() - from1.getTime()) * 1.0
					/ (60 * 60 * 1000);
		} else {
			temphour = (t1.getTime() - from1.getTime()) * 1.0
					/ (60 * 60 * 1000) - 2;
		}
		double temphour1 = 0;
		if (to1.getHours() <= 12) {
			temphour1 = (to1.getTime() - t2.getTime()) * 1.0 / (60 * 60 * 1000);
		} else {
			temphour1 = (to1.getTime() - t2.getTime()) * 1.0 / (60 * 60 * 1000)
					- 2;
		}
		System.out.println(temphour + " " + temphour1);
		System.out.println("总时间为：" + sum + temphour + temphour1);
	}

	/**
	 * 根据其实与接受时间计算这个时间段中所包含的工作日
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int calculateElapsedWorkDays(Date startDate, Date endDate) {
		return 0;
	}

	public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
		List<Date> lDate = new ArrayList<Date>();
		lDate.add(beginDate);// 把开始时间加入集合
		Calendar cal = Calendar.getInstance();
		// 使用给定的 Date 设置此 Calendar 的时间
		cal.setTime(beginDate);
		boolean bContinue = true;
		while (bContinue) {
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
			cal.add(Calendar.DAY_OF_MONTH, 1);
			// 测试此日期是否在指定日期之后
			if (endDate.after(cal.getTime())) {
				lDate.add(cal.getTime());
			} else {
				break;
			}
		}
		lDate.add(endDate);// 把结束时间加入集合
		return lDate;
	}
}
